var Admin = require('../lib/admin');
var Card = require('../lib/card');
var Sticker = require('../lib/sticker');
var Frame = require('../lib/frame');

module.exports = function(app) {

	//---------------tools-----------------------
	var Tools = require('../lib/tools');
	app.post('/v1/tools/image/upload', Tools.uploadImg); //上传图片
	app.post('/v1/tools/image/upload2', Tools.uploadImg2); //上传图片2
	//---------------admin-----------------------
	app.all('/admin/*', function(req, res, next) {
		var sys_user = req.session.sys_user;
		if (!sys_user) {
			return res.redirect('/');
		}
		next();
	})
	app.get('/', Admin.login); //登录
	app.post('/AM_login', Admin.login_send); //登录提交
	app.get('/admin/index', Admin.homepage); //首页

	//--------------todos------------------------
	app.post('/admin/todos/ajax', function(req, res) {
		res.json({
			status: true,
			resources: []
		})
	}); //待办事项

	app.get('/admin/tools/search', Admin.search);//搜索
	//--------------card-----------------------
	app.get('/admin/card/list', Card.manager_card_list_page); //card管理
	app.get('/admin/card/list/display', Card.manager_card_list_display_page); //card管理
	app.get('/admin/card/add', Card.manager_card_add_page); //新增card页面
	app.post('/admin/card/add', Card.manager_card_add); //新增card提交
	app.get('/admin/card/edit', Card.manager_card_edit_page); //修改card页面
	app.post('/admin/card/edit', Card.manager_card_edit); //修改card
	app.post('/admin/card/del', Card.manager_card_del); //删除card
	app.post('/admin/card/recommend', Card.manager_card_recommend); //推荐卡片

	//--------------sticker------------------------
	app.get('/admin/sticker/list', Sticker.manager_sticker_list_page); //贴纸管理页面
	app.get('/admin/sticker/add', Sticker.manager_sticker_add_page); //新增贴纸页面
	app.post('/admin/sticker/add', Sticker.manager_sticker_add); //新增贴纸
	app.get('/admin/sticker/edit', Sticker.manager_sticker_edit_page); //修改贴纸页面
	app.post('/admin/sticker/edit', Sticker.manager_sticker_edit); //修改贴纸
	app.post('/admin/sticker/del', Sticker.manager_sticker_del); //删除贴纸

	//--------------frame--------------------
	app.get('/admin/frame/list', Frame.manager_frame_list_page); //像框管理页面
	app.get('/admin/frame/add', Frame.manager_frame_add_page); //新增像框页面
	app.post('/admin/frame/add', Frame.manager_frame_add); //新增像框
	app.get('/admin/frame/edit', Frame.manager_frame_edit_page); //修改像框页面
	app.post('/admin/frame/edit', Frame.manager_frame_edit); //修改像框
	app.post('/admin/frame/del', Frame.manager_frame_del); //删除像框

}

function checkHeader(req, res, next) {
	var reqIp = req.get("x-real-ip") || req.get("x-forwarded-for") || req.ip;
	if (reqIp.substring(0, 7) == '::ffff:') {
		reqIp = reqIp.substring(7);
	}
	//校验IP
	if (reqIp != '127.0.0.1') {
		console.log(reqIp + '正在访问')
			//先校验sign
		var safekey = 'ae942b37c1b114c278a87b1e30d2daea'; //安全码
		var timestamp = req.headers.timestamp;
		var reqSign = req.headers.crpdb_auth;
		if (!timestamp || !reqSign) {
			console.warn(reqIp + ' missing signature')
			return res.end();
		}
		var md5 = crypto.createHash('md5');
		var signStr = 'timestamp=' + timestamp + '&safekey=' + safekey;
		var sign = md5.update(signStr).digest('hex').toLowerCase();
		if (sign !== reqSign) {
			console.warn(reqIp + ' signature verification error')
			return res.end();
		}
	}
	next();
}